package code16_反射;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author 黄衡熙
 * @version 1.0
 */
public class Test03_操作构造方法 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //获取当前类找那个所有public修饰的构造方法
      /*  Class c=User.class;
        Constructor[] cons=c.getConstructors();
        for(Constructor c1:cons){
            System.out.println(c1);
        }
        System.out.println("------------------------");
        //获取所有构造方法，不区分修饰符
        Constructor[] cons2=c.getDeclaredConstructors();
        for(Constructor c2:cons2){
            System.out.println(c2);
        }
        System.out.println("--------------------------------");
        //获取指定参数的public修饰符的构造方法
        Constructor cons3=c.getConstructor();
        System.out.println(cons3);
        constructor=c.getConstructors(Integer.class,String.class,String.class);
        System.out.println(cons3);
        constructor=c.getConstructor(Integer.class,String.class);
        System.out.println(cons3);
        // 如果不是public修饰的构造方法,则抛出异常
//        constructor = c.getConstructor(Integer.class,String.class);
//        System.out.println(constructor);
        System.out.println("***************************");

        // 4.获取指定参数的构造方法,不区分修饰符
        Constructor declaredConstructor = c.getDeclaredConstructor();
        System.out.println(declaredConstructor);
        declaredConstructor = c.getDeclaredConstructor(Integer.class);
        System.out.println(declaredConstructor);
        declaredConstructor = c.getDeclaredConstructor(Integer.class, String.class);
        System.out.println(declaredConstructor);
        declaredConstructor = c.getDeclaredConstructor(Integer.class, String.class, String.class);
        System.out.println(declaredConstructor);

        System.out.println("******************************");
        // 创建对象
        constructor = c.getConstructor();
        User user = (User) constructor.newInstance();
        System.out.println(user);
        constructor = c.getConstructor(Integer.class, String.class, String.class);
        user = (User) constructor.newInstance(1, "admin", "123456");
        System.out.println(user);

        //constructor = c.getConstructor(Integer.class);
        declaredConstructor = c.getDeclaredConstructor(Integer.class);
        declaredConstructor.setAccessible(true);
        user = (User) declaredConstructor.newInstance(1);
        System.out.println(user);
*/

    }
}
